//
//  SetViewController.m
//  花花时尚
//
//  Created by 霍文轩 on 15/9/1.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

#import "SetViewController.h"
#import "NightSetTableViewCell.h"
#import "NightMode.h"

@interface SetViewController () <UITableViewDataSource,UITableViewDelegate,NightSetTableViewCellDelegate>
@property (nonatomic,retain) UITableView * table;
@end

@implementation SetViewController
- (void)dealloc
{
    [_table release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置";
    self.navigationController.navigationBar.barStyle = [NightMode nightModeON]?UIBarStyleBlack:UIBarStyleDefault;
    self.navigationController.navigationBar.translucent = YES;
    
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];

    [self createViews];
}
-(void)createViews{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49) style:UITableViewStyleGrouped];
    _table.backgroundColor = [UIColor clearColor];
    _table.delegate = self;
    _table.dataSource = self;
    
    [self.view addSubview:_table];
    [_table release];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 1;
        case 1:
            return 1;
        default:
            return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 第一组第一行，夜间模式
    if(indexPath.section == 0 && indexPath.row == 0){
        NightSetTableViewCell * cell = [[NightSetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; // 不可以用重用池，会导致无法刷新字体颜色
        cell.textLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 1 && indexPath.row == 0){ // 第二组第一行，清空缓存
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.textLabel.text = @"清除缓存";
        cell.textLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}
#pragma mark - tableViewCell点击事件  清除缓存
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 清空library
    if(indexPath.section == 1 && indexPath.row == 0){
        NSFileManager * manager = [NSFileManager defaultManager];
        NSString * libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
        
        // 只删除自己创建的文件夹和SDWebImage图片缓存文件夹
        NSString * dailyPath = [NSString stringWithFormat:@"%@/%@",libraryPath,@"daily"];
        NSString * newsPath = [NSString stringWithFormat:@"%@/%@",libraryPath,@"news"];
        NSString * programaPath = [NSString stringWithFormat:@"%@/%@",libraryPath,@"programa"];
        NSString * SDWebImagePath = [NSString stringWithFormat:@"%@/%@",libraryPath,@"Caches/com.hackemist.SDWebImageCache.default"];
        [manager removeItemAtPath:dailyPath error:nil];
        [manager removeItemAtPath:newsPath error:nil];
        [manager removeItemAtPath:programaPath error:nil];
        [manager removeItemAtPath:SDWebImagePath error:nil];

        
        // 创建提示label
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
        label.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-100);
        label.text = @"清除成功";
        label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        label.textColor = [UIColor whiteColor];
        label.layer.cornerRadius = 10;
        label.clipsToBounds = YES; // 设置这个才会使圆角生效
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:17];
        [self.view addSubview:label];
        // 1s后让label消失
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(labelDisappear:) userInfo:@{@"label":label} repeats:NO];
        [label release];
    }
}
// 让提示label消失
-(void)labelDisappear:(NSTimer *)timer{
    UILabel * label = [timer.userInfo objectForKey:@"label"];
    [label removeFromSuperview];
}
@end
